﻿using ReflectionStudio.Core.Events;
using System;
using System.Data;
using System.Windows;
using System.Windows.Data;
using System.ComponentModel;
namespace WPFExcelReport
{
    public partial class TSQP12 : ViewBase
    {
        #region ==MERKM===========
        public static readonly DependencyProperty MERKMProperty = DependencyProperty.Register("MERKM",
            typeof(string), typeof(TSQP12),
            new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnMERKMChanged));
        public string MERKM
        {
            get
            {
                return (string)GetValue(MERKMProperty);
            }
            set
            {
                SetValue(MERKMProperty, value);
                ((VQPMERKM)MV).MERKM = value;
                Locate();
            }
        }
        private static void OnMERKMChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as TSQP12;
            if (src != null)
            {
                src.MERKM = (string)e.NewValue;
            }
        }
        #endregion

        #region ==KEY===========
        public static readonly DependencyProperty KEYProperty = DependencyProperty.Register("KEY",
            typeof(string), typeof(TSQP12),
            new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnKEYChanged));
        public string KEY
        {
            get
            {
                return (string)GetValue(KEYProperty);
            }
            set
            {
                SetValue(KEYProperty, value);
                ((VQPMERKM)MV).KEY = value; 
            }
        }
        private static void OnKEYChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as TSQP12;
            if (src != null)
            {
                src.KEY = (string)e.NewValue;
            }
        }
        #endregion

        #region ==dwQT===========
        public static readonly DependencyProperty dwQTProperty = DependencyProperty.Register("dwQT",
            typeof(DataRow), typeof(TSQP12),
            new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OdwQTChanged));
        public DataRow dwQT
        {
            get
            {
                return (DataRow)GetValue(dwQTProperty);
            }
            set
            {
                SetValue(dwQTProperty, value); 
            }
        }
        private static void OdwQTChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as TSQP12;
            if (src != null)
            {
                DataRow row = (DataRow)e.NewValue;
                src.dwQT = row;
            }
        }
        #endregion

        public TSQP12()
        {
            if (DesignerProperties.GetIsInDesignMode(this))
                return; 
            MV = new VQPMERKM();
            this.DataContext = MV; 
            InitializeComponent();
         }

        public override void vwMaster_CurrentChanged(object sender, EventArgs e)
        {
            base.vwMaster_CurrentChanged(sender, e);
            dwQT = dwMaster;
            if (dwQT != null)
            {
                KEY = dwQT["QPMMERKM"].ToString();
            }
            else
            {
                KEY = "";
            }
        }
      
        public override void Window_Loaded(object sender, RoutedEventArgs e)
        {
            if (DesignerProperties.GetIsInDesignMode(this))
                return;
            base.Window_Loaded(sender, e);
            Load();
        }

        public void Load()
        {
            dwData = TBMain.NewRow();
            dwData["QPMMANDANT"] = app.Mandant; 
            SResult = ((VQPMERKM)MV).QueryQPMERKM(dwData, -1);
            foreach (DataRow row in SResult.Tables[0].Rows)
            {
                row["DES"] = Helpers.Helper.GetGB2312Value(row["QPMKURZB"].ToString());
            }
            MergeData(SResult,false); 
        }

        public void Locate()
        {
            if (TBMain == null)
                return;
             int idx = -1; 
            var query = from DataRow row in TBMain.AsEnumerable()
                        where row["QPMMERKM"].ToString() == MERKM
                        select row;
            foreach (DataRow row in query)
            {
                 idx = TBMain.Rows.IndexOf(row);              
                break;
            }
            vwMaster.View.MoveCurrentTo(idx);
        }

        public override void ManageCommand(object sender, RoutedEventArgs e)
        {
            if (IsManage)
            {
                Load();
            }
            else
            {
                VICC_WFEVENTS.SVWF.QP12(this.RUNID);
            }
            base.ManageCommand(sender, e);
        }
    }
}
